package jgl.vajra.kingkong.part.vehicle.activity;

import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.gyf.immersionbar.ImmersionBar;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.bean.HomeCarModelBean;
import jgl.vajra.kingkong.bean.HomeMechanicsBean;
import jgl.vajra.kingkong.bean.HomePlaceBean;
import jgl.vajra.kingkong.databinding.ActivityPriceCalculatorBinding;
import jgl.vajra.kingkong.event.CityChoiceEvent;
import jgl.vajra.kingkong.manager.FragmentManager;
import jgl.vajra.kingkong.manager.IntentManager;
import jgl.vajra.kingkong.part.home.activity.HomeActivity;
import jgl.vajra.kingkong.part.home.fragment.HomeVehicleTypeFragment;
import jgl.vajra.kingkong.part.home.pop.AccountingMethodPop;
import jgl.vajra.kingkong.part.home.pop.AllCarTypePop;
import jgl.vajra.kingkong.part.home.pop.AllMachineryPop;
import jgl.vajra.kingkong.part.vehicle.contract.PriceCalculatorContract;
import jgl.vajra.kingkong.part.vehicle.viewModel.PriceCalculatorViewModel;
import jgl.vajra.kingkong.utils.RandomUtils;
import jgl.vajra.kingkong.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.kingkong.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.kingkong.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.kingkong.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.kingkong.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.kingkong.widget.utils.ToastUtils;
import jgl.vajra.kingkong.widget.utils.UIUtils;

@CreateViewModel(PriceCalculatorViewModel.class)
public class PriceCalculatorActivity extends BaseMVVMActivity<PriceCalculatorViewModel, ActivityPriceCalculatorBinding> implements PriceCalculatorContract.View, BaseBindingItemPresenter {

    private SingleTypeBindingAdapter adapterMechanics;
    private List<HomeMechanicsBean> listMechanics = new ArrayList<>();
    private AllMachineryPop allMachineryPop;
    private AccountingMethodPop accountingMethodPop;
    private List<HomeCarModelBean> carModelBean;
    private AllCarTypePop allCarTypePop;
    private List<HomePlaceBean> listPlace = new ArrayList<>();
    private SingleTypeBindingAdapter adapterPlace;
    private String area;

    @Override
    public int getLayoutId() {
        return R.layout.activity_price_calculator;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);
        ImmersionBar.with(this).statusBarColor(R.color.color_012BB2).navigationBarColor(R.color.white).fitsSystemWindows(true).statusBarDarkFont(false, 0.2f).init();


        mBinding.toolbar.setNavigationIcon(R.drawable.ic_left_arrow_white);
        mBinding.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        requestNetData();

        //机械类型
        // 不可嵌套滑动
        mBinding.recyclerViewMechanics.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerViewMechanics.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerViewMechanics.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        adapterMechanics = new SingleTypeBindingAdapter(this, listMechanics, R.layout.item_price_calculator_mechanics);
        mBinding.recyclerViewMechanics.setAdapter(adapterMechanics);
        adapterMechanics.setItemPresenter(this);

    }

    public void requestNetData() {
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AEACCoder.encrypt(RandomUtils.getRandom()));
        mViewModel.getCarTypeData(map);
    }

    public void getCarModelData(String type) {
        Map<String, Object> mapCarModel = new HashMap<>();
        mapCarModel.put("Authorization", AEACCoder.encrypt(RandomUtils.getRandom()));
        mapCarModel.put("type", type);
        mViewModel.getCarModelData(mapCarModel);
    }

    //位置
    @Override
    public void onTriangle() {
        //IntentManager.toCityChoiceActivity(this);
    }

    //全部机械
    @Override
    public void onAllMachinery() {
        if (allMachineryPop == null)
            allMachineryPop = new AllMachineryPop(this, listMechanics);
        allMachineryPop.setAllMachineryListener((position, itemData) -> {
            if (listMechanics.get(position).isClick) {
                return;
            }
            for (int i = 0; i < listMechanics.size(); i++) {
                listMechanics.get(i).isClick = false;
            }

            listMechanics.get(position).isClick = true;
            adapterMechanics.refresh();

            mBinding.tabLayout.getTabLayout().removeAllTabs();
            getCarModelData(itemData.type);
        });
        allMachineryPop.showPopupWindow(mBinding.toolbar);
    }

    //全部车型
    @Override
    public void onAllVehicle() {
        allCarTypePop = new AllCarTypePop(this, carModelBean);
        allCarTypePop.showPopupWindow(mBinding.tvAllMachinery);
        allCarTypePop.setAllCarTypeListener((position, itemData) -> {
            mBinding.tabLayout.setCurrentItem(position);
        });
    }

    @Override
    public void onDeletePlace(int position) {
        listPlace.remove(position);
        adapterPlace.refresh();
    }

    //算账方式
    @Override
    public void onSettleAccounts() {
        if (carModelBean == null)
            return;

        accountingMethodPop = new AccountingMethodPop(this, carModelBean.get(mBinding.tabLayout.getTabLayout().getSelectedTabPosition()).model, area);
        accountingMethodPop.showPopupWindow(mBinding.tvAllMachinery);
    }

    @Override
    public void onClickMechanics(int position, HomeMechanicsBean itemData) {
        if (itemData.isClick)
            return;

        for (int i = 0; i < listMechanics.size(); i++) {
            listMechanics.get(i).isClick = false;
        }
        listMechanics.get(position).isClick = true;
        adapterMechanics.refresh();

        mBinding.tabLayout.getTabLayout().removeAllTabs();

        getCarModelData(itemData.type);
    }

    @Override
    public void returnHomeMechanicsBean(List<HomeMechanicsBean> homeMechanicsBean) {
        if (homeMechanicsBean != null && homeMechanicsBean.size() != 0) {
            homeMechanicsBean.get(0).isClick = true;
            listMechanics.addAll(homeMechanicsBean);
            adapterMechanics.refresh();

            getCarModelData(homeMechanicsBean.get(0).type);
        }

    }

    @Override
    public void returnHomeCarModelBean(List<HomeCarModelBean> data) {
        carModelBean = data;
        List<String> titleList = new ArrayList<>();
        List<Fragment> fragmentList = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            titleList.add(data.get(i).describe);
            HomeVehicleTypeFragment listFragment = FragmentManager.getHomeVehicleTypeFragment(i, data.get(i).remark, data.get(i).model, false);
            fragmentList.add(listFragment);
        }
        mBinding.tabLayout.setCustomViewTabLayoutView(this.getSupportFragmentManager(), titleList, fragmentList,true);
    }

    @Override
    public void onItemClick(int position, Object itemData) {

    }

    @Override
    protected boolean isRegisterEvent() {
        return true;
    }

    //选中的开放城市
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCityChoiceEvent(CityChoiceEvent event) {
        area = event.itemData.areaCode;
        mBinding.tvAddress.setText(event.itemData.abbreviation);

//        resetData();
    }
}
